<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Http\Controllers;

use App\Common\Logger;
use App\Logic\PayCallBackLogic;
use function Composer\Autoload\includeFile;
use Illuminate\Support\Facades\DB;

includeFile(app_path('Logic/WeiChatPay/WxPayPubHelper.php'));

class WeChatApplyPayCallBack
{

    public function handle()
    {
        DB::beginTransaction();
        try {
            //使用通用通知接口
            $notify = new \Notify_pub();
            //存储微信的回调
            $xml = empty($GLOBALS['HTTP_RAW_POST_DATA']) ? file_get_contents('php://input') : $GLOBALS['HTTP_RAW_POST_DATA'];
            //以log文件形式记录回调信息
            Logger::instance()->wxPayCallback("【接收到的notify通知】:\n" . $xml . "\n");
            //处理XML
            $notify->saveData($xml);

            if ($notify->checkSign() == FALSE) {
                $notify->setReturnParameter("return_code", "FAIL");//返回状态码
                $notify->setReturnParameter("return_msg", "签名失败");//返回信息
            } else {
                $notify->setReturnParameter("return_code", "SUCCESS");//设置返回码
            }

            //验证签名，并回应微信。
            //对后台通知交互时，如果微信收到商户的应答不是成功或超时，微信认为通知失败，
            //微信会通过一定的策略（如30分钟共8次）定期重新发起通知，
            //尽可能提高通知的成功率，但微信不保证通知最终能成功。
            $returnXml = $notify->returnXml();
            Logger::instance()->wxPayCallback("【响应给微信的内容】:\n" . $returnXml . "\n");
            echo $returnXml;

            if ($notify->checkSign() == TRUE) {
                if ($notify->data["return_code"] == "FAIL") {
                    //此处应该更新一下订单状态，商户自行增删操作
                    $err_msg = $notify->data["return_msg"];
                    Logger::instance()->wxPayCallback("【通信出错】:\n" . $xml . "\n");
                    PayCallBackLogic::instance()->updatePay($notify->data["out_trade_no"], $err_msg);
                } elseif ($notify->data["result_code"] == "FAIL") {
                    //此处应该更新一下订单状态，商户自行增删操作
                    $err_msg = $notify->data["err_code"];
                    Logger::instance()->wxPayCallback("【业务出错】:\n" . $xml . "\n");
                    PayCallBackLogic::instance()->updatePay($notify->data["out_trade_no"], $err_msg);
                } else {
                    PayCallBackLogic::instance()->handle($notify->data["out_trade_no"], $xml);
                }
            } else {
                Logger::instance()->wxPayCallback("【校验签名失败】:\n" . $xml . "\n");
            }
        } catch (\Exception $exception) {
            DB::rollBack();
            Logger::instance()->setErrorLog($exception);
        }

    }

}